package com.android.mg.db;

import java.util.ArrayList;

import com.android.mg.beans.Paraderos;

import android.app.Service;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Binder;
import android.os.IBinder;
import android.provider.BaseColumns;

public class DBAdapter extends Service {

	private final IBinder mBinder = new LocalBinder();
	
	private DBHelper dbHelper;
	
	private SQLiteDatabase db;
	
	public class LocalBinder extends Binder {
		public DBAdapter getService(){
			return DBAdapter.this;
		}
	}
	
	
	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}


	@Override
	public void onCreate() {
		dbHelper = new DBHelper(this);
		
		db = dbHelper.getDataBase();
	}


	@Override
	public void onDestroy() {
		db.close();
	}
	
	/*Obtiene un cursor con todos los paraderos de la base de datos*/
	public Cursor getCursorParaderos(){
		String[] cols = {"id","longitu","latitud","corredor", "tap","nombre"};
		return db.query("paraderos",cols , null, null, null, null, BaseColumns._ID);
	}
	
	
	public ArrayList<Paraderos> getAllParaderos(){
		ArrayList<Paraderos> paraderos = new ArrayList<Paraderos>();
		Cursor result = getCursorParaderos();
		
		if(result.moveToFirst())
			do{
				paraderos.add(new Paraderos(result.getInt(result.getColumnIndex("id")),
						result.getString(result.getColumnIndex("longitud")),
						result.getString(result.getColumnIndex("latitud")),
						result.getString(result.getColumnIndex("corredor")),
						result.getString(result.getColumnIndex("tap")),
						result.getString(result.getColumnIndex("nombre"))));
			}while(result.moveToNext());
		
		return paraderos;
	}
	

}
